'use strict';

var app = angular.
  module('phonecatApp').
  config(['$locationProvider' ,'$routeProvider',
    function config($locationProvider, $routeProvider) {
      $routeProvider.
        when('/login',{
          template: '<admin-login></admin-login>'
        }).

        when('/',{
          template: '<user-list></user-list>'
       }).

        when('/user_list',{
          template : '<user-list ></user-list>'
        }). 
        when('/follower_list',{
          template : '<follower-list ></follower-list>'
        }). 
        when('/comment_list',{
          template : '<comment-list ></comment-list>'
        }).  
        when('/all_user',{
          template : '<all-user ></all-user>'
        }).   
        when('/console',{
          template : '<console></console>'
        }).
        when('/my_account',{
          template : '<my-account></my-account>'
        }).
        when('/search_account',{
          template : '<search-account></search-account>'
        }).
        when('/other_account',{
          template : '<other-account></other-account>'
        }).
        when('/send_newuser',{
          template : '<send-newuser></send-newuser>'
        }).

                 
        otherwise('/')

      $locationProvider.html5Mode({
        enabled: true,
        requireBase: false
      });



    }

  ]);


app.factory('convert', function () {
  return {
    base64ToArrayBuffer: function (base64) {
      var binary_string = window.atob(base64);
      var len = binary_string.length;
      var bytes = new Uint8Array(len);
      for (var i = 0; i < len; i++) {
        bytes[i] = binary_string.charCodeAt(i);
      }
      return bytes.buffer;
    }
  }
})



// app.directive('customOnChange', function() {
//   return {
//     restrict: 'A',
//     link: function (scope, element, attrs) {
//       console.log('what hadhsahs')
//       var onChangeFunc = scope.$eval(attrs.customOnChange);
//       element.bind('change', onChangeFunc);
//     }
//   };
// });

// app
//   .directive('excelExport',
//     function () {
//       return {
//         restrict: 'A',
//         scope: {
//           fileName: "@",
//             data: "&exportData"
//         },
//         replace: true,
//         template: '<button class="btn btn-primary btn-ef btn-ef-3 btn-ef-3c mb-10" ng-click="download()">Export to Excel <i class="fa fa-download"></i></button>',
//         link: function (scope, element) {
          

        
//         }
//       };
//     }
//  );


// app.factory('expenseModal', function (btfModal) {
//   return btfModal({
//     controller: 'modalCtrl',
//     templateUrl: 'views/app/expense-modal/expense-modal.template.html'
//   });
// })

// app.factory('couponModal', function (btfModal) {
//   return btfModal({
//     controller: 'couponModal',
//     templateUrl: 'views/app/coupon-modal/coupon-modal.template.html'
//   });
// })


app.factory('activityModal', function (btfModal) {
  return btfModal({
    controller: 'activityModal',
    templateUrl: 'views/app/activity-modal/activity-modal.template.html'
  });
})


app.run(['$rootScope', function($rootScope) {
  $rootScope.title = ''
    $rootScope.$on('$routeChangeSuccess', function (event, current, previous) {
       // $rootScope.title="xx"
      //  $rootScope.title = current.$$route.title;
    });
}]);


// app.run(["moment", function(moment){
//     moment.locale("zh-cn");
// }]);


  // angular.module('phonecatApp').controller('xxxx',function($cookies,$location){
  //   if ($cookies.get('userid') != undefined){
  //     if ($cookies.get('userid').length != 0){
  //       if ($cookies.get('usertype') == undefined){
  //         $location.path('/registertype')
  //       }
  //       else{
  //         $location.path( "/" );
  //       }
              
  //     }
  //   }
    
  // })

// app.factory('mainpage', function (btfModal) {
//   return btfModal({
//     controller: 'mainpageModal',
//     templateUrl: 'views/app/mainpage-modal/mainpage-modal.template.html'
//   });
// })



  app.controller('RedirectToMain',function($cookies,$location){
    if ($cookies.get('userid') != undefined){
      if ($cookies.get('userid').length != 0){
        if ($cookies.get('usertype') == undefined){
          $location.path('/registertype')
        }
        else{
          $location.path( "/" );
        }
              
      }
    }
    
  })

  app.controller('CheckUserType',function($cookies,$location){
    if (($cookies.get('usertype') != undefined) || ($cookies.get('userid') == undefined)){
      $location.path( "/" );
    }
    else if ($cookies.get('userid').length == 0){
      $location.path('/')
    }
  })

  app.controller('NotOwner',function($cookies,$location){
    if ($cookkies.get('usertype') != undefined){
    if ($cookies.get('usertype') != "owner"){
      $location.path( "/" );
    }
  }
  else{
    $location.path( "/" );
  }
  })

app.directive('elastic', [
    '$timeout',
    function($timeout) {
        return {
            restrict: 'A',
            link: function($scope, element) {
                $scope.initialHeight = $scope.initialHeight || element[0].style.height;
                var resize = function() {
                    element[0].style.height = $scope.initialHeight;
                    element[0].style.height = "" + element[0].scrollHeight + "px";
                };
                element.on("input change", resize);
                $timeout(resize, 0);
            }
        };
    }
]);

  app.directive('fallbackSrc', function () {
  var fallbackSrc = {
    link: function postLink(scope, iElement, iAttrs) {
      iElement.bind('error', function() {
        angular.element(this).attr("src", iAttrs.fallbackSrc);
      });
    }
   }
   return fallbackSrc;
});

